package org.wattdepot.cli.commands;

import java.util.List;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.jaxb.SubSources;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Commands for list summary.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class ListSummary extends UserCommandInterface implements UserCommands {

  @Override
  public String doCommand(String args) {
    String[] inputString = args.split(" ");
    String output = null;
    try {
      output = listSummary(inputString[1]);
    }
    catch (WattDepotClientException e) {
      System.out.println("List summary error.");
    }
    return output;
  }

  /**
   * Prints information about a single source.
   * 
   * @param sourceName Name of one of the public sources is the system.
   * @throws WattDepotClientException exception that is thrown
   * @return List of specific source summary.
   */
  public String listSummary(String sourceName) throws WattDepotClientException {

    // check if source with sourceName exists
    if (!sourceExists(sourceName)) {
      // exit method
      return "";
    }

    // gets the desired source
    Source source = getClient().getSource(sourceName);
    // gets the source summary of specified source
    SourceSummary sourceSummary = getClient().getSourceSummary(sourceName);
    // the subSources string that will be displayed
    StringBuffer subSources = new StringBuffer();
    // the returned subSources String
    StringBuffer returnedSubSources = new StringBuffer(256);
    // the owner string that will be displayed
    String owner;
    // used to check if subsources exist
    SubSources checkSources = source.getSubSources();

    // if subsource is not null, populate the subSources string
    if (checkSources == null) {
      subSources.append("none");
    }
    else {
      // gets the entire subsource list then appends
      List<String> subSourcesList = source.getSubSources().getHref();
      for (String subSourceURL : subSourcesList) {
        String[] subSource = subSourceURL.split("/");
        subSources.append(subSource[subSource.length - 1] + ", ");
      }
    }

    // splits the owner string up
    String[] ownerArray = source.getOwner().split("/");
    // puts the owner into the string that will be displayed
    owner = ownerArray[ownerArray.length - 1];

    // displays all the details of each field
    returnedSubSources.append("SubSources: " + subSources.toString() + "\n" + "Description: "
        + source.getDescription() + "\n" + "Owner: " + owner + "\n" + "Location: "
        + source.getLocation() + "\n" + "Coordinates: " + source.getCoordinates() + "\n"
        + "Properties: " + source.getProperties() + "\n" + "Earliest data: "
        + sourceSummary.getFirstSensorData() + "\n" + "Lastest data: "
        + sourceSummary.getLastSensorData() + "\n" + "Total data points: "
        + sourceSummary.getTotalSensorDatas() + "\n");

    return returnedSubSources.toString();

  }

}
